#include "stdafx.h"
#include "GameSound.h"


GameSound::GameSound()
{
	m_hSample = 0;
	m_hChannel = 0;
}


GameSound::~GameSound()
{
	if (m_hSample) Free();
}

void GameSound::Load(const char *file)
{
	if (m_hSample) Free();
	m_hSample = BASS_SampleLoad(false, file, 0, 0, 8, 0);
	m_hChannel = BASS_SampleGetChannel(m_hSample, false);
}

void GameSound::Play(const char *file, float speed, bool loop)
{
	if (m_hSample) Free();
	m_hSample = BASS_SampleLoad(false, file, 0, 0, 8, loop ? BASS_SAMPLE_LOOP : 0);
	m_hChannel = BASS_SampleGetChannel(m_hSample, false);
	if (speed > 0.2f)
	{
		float freq = 0;
		auto fok = BASS_ChannelGetAttribute(m_hChannel, BASS_ATTRIB_FREQ, &freq);
		fok = BASS_ChannelSetAttribute(m_hChannel, BASS_ATTRIB_FREQ, freq * speed);
	}
	auto fok = BASS_ChannelPlay(m_hChannel, true);
}

void GameSound::Play()
{
	auto fok = BASS_ChannelPlay(m_hChannel, true);
}

void GameSound::Free()
{
	BASS_ChannelStop(m_hChannel);
	BASS_SampleFree(m_hSample);
	m_hSample = 0;
	m_hChannel = 0;
}